<?php
declare(strict_types=1);

namespace app\common\business;

use app\common\model\Ad as AdModel;
use app\common\model\AdPosition as AdPositionModel;
use think\facade\Log;

class AdPosition extends CommonBase
{
    private $_adPositionObj = null;

    public function __construct(string $websiteCode)
    {
        $this->_adPositionObj = new AdPositionModel([], $websiteCode);
        $this->_websiteCode   = $websiteCode;
    }

    public function getAllAdPosition(array $where = []): array
    {
        $rows = [];
        try {
            $rows = $this->_adPositionObj->getAllAdPosition($where);
        } catch (\Exception $e) {
            logErr('读取所有广告位置失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return $rows ? $rows->toArray() : [];
    }

    public function getAdPositionList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $rows  = [];
        $count = 0;
        try {
            $rows  = $this->_adPositionObj->getAdPositionList($page, $limit, $where);
            $count = $this->_adPositionObj->getAdPositionCount($where);
        } catch (\Exception $e) {
            logErr('读取广告位置列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return ['rows' => $rows ? $rows->toArray() : [], 'count' => $count];
    }

    public function addAdPosition(array $data): array
    {
        try {
            return [self::_RESULT => $this->_adPositionObj->addAdPosition($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加广告位置失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findAdPositionById(int $id): array
    {
        $adPosition = null;
        try {
            $adPosition = $this->_adPositionObj->findAdPositionById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取广告位置详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($adPosition)) {
            return [];
        }

        return $adPosition->toArray();
    }

    public function updateAdPosition(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_adPositionObj->updateAdPosition($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新广告位置信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteAdPosition(int $id): array
    {
        try {
            $adModel = new AdModel([], $this->_websiteCode);
            $adObj   = $adModel->getAdCount(['position_id' => $id]);
            if ($adObj) {
                return [self::_RESULT => false, self::_MESSAGE => '图片位置使用中，无法删除'];
            }

            $res = $this->_adPositionObj->deleteAdPosition($id);
            return [self::_RESULT => $res, self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除广告位置失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }
}
